﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


using NGE;

namespace MH.Server
{
    
    public partial class ServerWindow : Form
    {
        public ServerWindow()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ui_start.Enabled = true;
            ui_stop.Enabled = false;

            game = new Game("null", null);
            game.Unmount();
            ServerCOM = new ServerComponent(game);
            ServerCOM.AppendLog += new ServerComponent.LogEvent(ServerCOM_AppendLog);
            game.Mount(ServerCOM);
            game.Start();

            uistart();

        }

        void ServerCOM_AppendLog(object sender, string text)
        {
            append(text);
        }

        Game game;
        ServerComponent ServerCOM;

        //void boardcast_pcs_scenename(PlayerCharacter pc, int pcindex)
        //{
        //    List<int> sameSCNIndex = getPCIndexsInSameScene(pc, pcindex);

        //    foreach (int i in sameSCNIndex)
        //    {
        //        PlayerCharacterEventArgs pe = new PlayerCharacterEventArgs(PlayerCharacterActs.SETOPC, pc.InSceneName);
        //        server.Send(server.GetSession(i), pe);
        //        append(server.GetSession(i), "已向[ " + server.GetSession(i).Handle + " ]发出[ " + server.GetSession(pcindex).Handle + " ]的场景名");
        //    }
        //}

        void updateui()
        {
            try
            {
                listView1.Items.Clear();
                foreach (SCNPCs spcs in ServerCOM.pcs.Items)
                {
                    foreach (Character pc in spcs.Items)
                    {
                        listView1.Items.Add(spcs.Name + " [" + pc.Name + "] " + pc.Location);
                    }
                }
                //foreach (PlayerCharacter opc in ServerCOM.OPCs)
                //{
                //    listView1.Items.Add("[" + opc.Name + "] [" + opc.InSceneName + "] " + opc.Location);
                //}
            }
            catch
            { }
        }

        void uistart()
        {
            ServerCOM.Start(int.Parse(ui_port.Text));

            ui_port.Enabled = false;
            ui_start.Enabled = false;
            ui_stop.Enabled = true;
        }

        private void ui_start_Click(object sender, EventArgs e)
        {
            uistart();
        }

        private void ui_stop_Click(object sender, EventArgs e)
        {
            ServerCOM.Stop();
            ui_port.Enabled = true;
            ui_start.Enabled = true;
            ui_stop.Enabled = false;
        }

        private void ui_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void ui_hide_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            updateui();
        }


        private delegate void OutputDelegate(string text);
        private void append(string text)
        {
            if (this.InvokeRequired)
            {
                OutputDelegate outputdelegate = new OutputDelegate(append);
                Invoke(outputdelegate, text);
            }
            else
            {
                richTextBox1.AppendText(text + "\n");
                richTextBox1.ScrollToCaret();
            }
        }

        //private delegate void OutputDelegate2(Net.Session s, string text);
        //private void append(Net.Session s, string text)
        //{
        //    if (this.InvokeRequired)
        //    {
        //        OutputDelegate2 outputdelegate = new OutputDelegate2(append);
        //        Invoke(outputdelegate, s, text);
        //    }
        //    else
        //    {
        //        richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + ": [ID=" + s.Handle + "] " + text + "\n");
        //        richTextBox1.ScrollToCaret();
        //    }
        //}
    }
}
